package fginlayoutgen

import (
	"fginTpl/fgin_layout_gen/config"
	genhandle "fginTpl/fgin_layout_gen/gen_handle"
	"fginTpl/fgin_layout_gen/globals"
	"fginTpl/utils"
	"fmt"
	"os/exec"
	"path/filepath"
)

func GenFGinLayout(projectName, path string) {
	// 检查protoc命令是否存在
	_, err := exec.LookPath("protoc")
	if err != nil {
		fmt.Println("'protoc'不存在,无法生成grpc桩代码。请下载protoc")
		return
	}
	config.ReadConfig(projectName, path, "8090", "8091")
	absPath := utils.GetAbsPath(config.ConfigData.Path)
	baseDir := filepath.Join(absPath, "./"+config.ConfigData.ProjectName)
	globals.BASEDIR = baseDir
	// 检查如果已存在，则不进行覆盖
	if utils.FileOrDirExists(baseDir) {
		fmt.Println("fgin layout模版已存在, 路径为：", baseDir)
		return
	}
	// 全部目录路径
	dirs := []string{
		baseDir,
		filepath.Join(baseDir, "./cmd"),
		filepath.Join(baseDir, "./configs"),
		filepath.Join(baseDir, "./internal"),
		filepath.Join(baseDir, "./pb"),
		filepath.Join(baseDir, "./internal/controller"),
		filepath.Join(baseDir, "./internal/router"),
		filepath.Join(baseDir, "./internal/service"),
	}
	// 不存在则新增目录
	for _, dirv := range dirs {
		if err := utils.MakeDirAll(dirv); err != nil {
			fmt.Printf("创建目录失败: %s, 错误: %s\n", dirv, err.Error())
			continue
		}
	}
	// 实现代码生成
	genhandle.GenAll()
	fmt.Println("fgin layout模版创建完成, 路径为：", baseDir)
}

// v2 layout模版
func GenFGinLayoutV2(projectName, path string) {
	// 检查protoc命令是否存在
	_, err := exec.LookPath("protoc")
	if err != nil {
		fmt.Println("'protoc'不存在,无法生成grpc桩代码。请下载protoc")
		return
	}
	config.ReadConfig(projectName, path, "8090", "8091")
	absPath := utils.GetAbsPath(config.ConfigData.Path)
	baseDir := filepath.Join(absPath, "./"+config.ConfigData.ProjectName)
	globals.BASEDIR = baseDir
	// 检查如果已存在，则不进行覆盖
	if utils.FileOrDirExists(baseDir) {
		fmt.Println("fgin layout v2模版已存在, 路径为：", baseDir)
		return
	}
	// 全部目录路径
	dirs := []string{
		baseDir,
		filepath.Join(baseDir, "./cmd"),
		filepath.Join(baseDir, "./configs"),
		filepath.Join(baseDir, "./internal"),
		filepath.Join(baseDir, "./pb"),
		filepath.Join(baseDir, "./internal/controller"),
		filepath.Join(baseDir, "./internal/router"),
		filepath.Join(baseDir, "./internal/service"),
	}
	// 不存在则新增目录
	for _, dirv := range dirs {
		if err := utils.MakeDirAll(dirv); err != nil {
			fmt.Printf("创建目录失败: %s, 错误: %s\n", dirv, err.Error())
			continue
		}
	}
	// 实现代码生成
	genhandle.GenAllV2()
	fmt.Println("fgin layout v2模版创建完成, 路径为：", baseDir)
}

// http layout模版
func GenFGinLayoutHttp(projectName, path string) {
	config.ReadConfig(projectName, path, "", "")
	absPath := utils.GetAbsPath(config.ConfigData.Path)
	baseDir := filepath.Join(absPath, "./"+config.ConfigData.ProjectName)
	globals.BASEDIR = baseDir
	// 检查如果已存在，则不进行覆盖
	if utils.FileOrDirExists(baseDir) {
		fmt.Println("fgin layout http模版已存在, 路径为：", baseDir)
		return
	}
	// 全部目录路径
	dirs := []string{
		baseDir,
		filepath.Join(baseDir, "./cmd"),
		filepath.Join(baseDir, "./configs"),
		filepath.Join(baseDir, "./internal"),
		filepath.Join(baseDir, "./internal/controller"),
		filepath.Join(baseDir, "./internal/router"),
		filepath.Join(baseDir, "./internal/service"),
		filepath.Join(baseDir, "./internal/validator"),
	}
	// 不存在则新增目录
	for _, dirv := range dirs {
		if err := utils.MakeDirAll(dirv); err != nil {
			fmt.Printf("创建目录失败: %s, 错误: %s\n", dirv, err.Error())
			continue
		}
	}
	// 实现代码生成
	genhandle.GenAllHttp()
	fmt.Println("fgin layout http模版创建完成, 路径为：", baseDir)
}
